var tfObj ={
    "transforms": [
        {
            "header": {
                "stamp": {
                    "secs": 1709866099,
                    "nsecs": 53413198
                },
                "frame_id": "map",
                "seq": 0
            },
            "transform": {
                "translation": {
                    "y": 0,
                    "x": 0,
                    "z": 0
                },
                "rotation": {
                    "y": 0,
                    "x": 0,
                    "z": 0,
                    "w": 1
                }
            },
            "child_frame_id": "map"
        }
    ]
};
// myTest();


function myTest(e) {
	console.log('load test js');
	var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 217, 218, 215, 255];
	let res = testGetRgb(arr, 16);
	testColormap(res);
}

function testGetRgb(arr, base) {
	let argb = [arr[base + 2], arr[base + 1], arr[base]];
	var res = (argb[0] << 16) | (argb[1] << 8) | (argb[2]);

	// console.log('%o, int=%d',argb,res);

	return res;
}


function testColormap(rgba) {
	// var color = ;
	// console.log('testColormap rgba:%o, res %o,%o',rgba,color,typeof(color));
	return new THREE.Color(rgba);
}




/**
 * 绘制离线数据
 * @param {*} cloudPointObj 
 */
function drawDemo(cloudPointObj,viewObj,offset) {
	let startTime = new Date().getTime();
	let multiNum =1;//倍数，原始测试数据 data2 57654*32
	console.log(formatDate(startTime)+' start drawDemo %o', cloudPointObj);
	var msg = mockMsg;

	var uint8Arr = new Uint8Array(msg.data.length * multiNum);

	for (var n = 0; n < multiNum; n++) {
		 

		for (var i = 0; i < msg.data.length; i++) {
			let nnBase = msg.data.length * n;//倍数基准点
			uint8Arr[nnBase + i] = msg.data[i];

			// n= i%255;
			if (offset > 0) {
				if (i > msg.point_step && (i % msg.point_step - 1) === 0) {
					//修改相关参数
					// console.log('i/msg.point_step-1)===0  i=%d',i)
					let subBase = nnBase + i - msg.point_step;
					uint8Arr[subBase + 4] = uint8Arr[subBase + 4] + 1 *  n;//y
					uint8Arr[subBase + 5] = uint8Arr[subBase + 5] + 1 * offset%20;
					uint8Arr[subBase + 6] = uint8Arr[subBase + 6] + 1 *  n;
					uint8Arr[subBase + 7] = uint8Arr[subBase + 7] + 1 * n;

					uint8Arr[subBase + 16] = uint8Arr[subBase + 16] + 30 * offset;//B
					// uint8Arr[subBase+1]=30*n;
					// uint8Arr[subBase+2]=30*n;
					// uint8Arr[subBase+3]=30*n;
				}
			}

			uint8Arr[nnBase]
		}
	}



	msg.data = uint8Arr;//mockmsg也跟着变了，指针型
	msg.width = msg.data.length / msg.point_step;
	cloudPointObj.processMessage(msg);
	let endTime = new Date().getTime();
	console.log(formatDate(endTime)+' end drawDemo %o ', msg);
	console.log("duration is :"+(endTime-startTime)+" ms")
	

}


function formatDate(value) {
	var date;
	if(value!=undefined || value!=null){
		date = new Date(value);
	}else{
		date = new Date();
	}
	var y = date.getFullYear(),
	  m = date.getMonth() + 1,
	  d = date.getDate(),
	  h = date.getHours(),
	  i = date.getMinutes(),
	  s = date.getSeconds();
	  ms = date.getMilliseconds();
	if (m < 10) { m = '0' + m; }
	if (d < 10) { d = '0' + d; }
	if (h < 10) { h = '0' + h; }
	if (i < 10) { i = '0' + i; }
	if (s < 10) { s = '0' + s; }
	var t = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s+" "+ms;
	return t;
  }
function mouseEvent(view,cloudObj) {
	viewObj= view;
	cloudPointObj = cloudObj;
//	document.getElementById('viewer').addEventListener('click', onClick, false)
	document.getElementById('viewer').addEventListener('mousedown', mouseDown, false )
	document.getElementById('viewer').addEventListener('touchstart', touchDown, false )
	document.getElementById('viewer').addEventListener('mouseup', mouseUp, false)
    document.getElementById('viewer').addEventListener('touchend', touchUp, false)
}



var viewObj ;
var pointsObj;
var cloudPointObj;
objects = [];
var downX;
var downY;
var upX;
var upY;

//全局箭头对象
var globalAxeObj;
function touchDown(event){
//    event.preventDefault();
	downX = event.touches[0].clientX;
	downY = event.touches[0].clientY;
	console.log('touchDown ,clientX %d,clientY %d',downX,downY);
}

function touchUp(event){
    upX = event.changedTouches[0].clientX;
	upY = event.changedTouches[0].clientY;
//	console.log('touchUp clientX:'+upX+",clientY:"+upY);
	if(Math.abs(upX-downX)>5||Math.abs(upY-downY)>5){//鼠标移动超过5像素不视为点击操作
		return

	}
	onClick(event)
}

function mouseDown(event){
//    event.preventDefault();
	downX = event.clientX;
	downY = event.clientY;
	console.log(event)
	// console.log('mouseDown ,clientX %d,clientY %d',downX,downY);
}

function mouseUp(event){
//    event.preventDefault();
	upX = event.clientX;
	upY = event.clientY;
//	console.log('mouseUp %o ,clientX %o,clientY %o',cloudPointObj.mouse,event.clientX,event.clientY);
	if(Math.abs(event.clientX-downX)>5||Math.abs(event.clientY-downY)>5){//鼠标移动超过5像素不视为点击操作
		return

	}
	onClick(event)
}

function onClick(event) {
//    event.preventDefault();
	if(globalMode!=MODE_MARK){
		return
	}

	if(Math.abs(event.clientX-downX)>5||Math.abs(event.clientY-downY)>5){//鼠标移动超过5像素不视为点击操作
		return
	}

	cloudPointObj.mouse.x = (upX / viewObj.renderer.domElement.clientWidth) * 2 - 1;
	cloudPointObj.mouse.y = - (upY / viewObj.renderer.domElement.clientHeight) * 2 + 1;

	// console.log('onClick %o ,clientX %o,clientY %o',cloudPointObj.mouse,event.clientX,event.clientY);
	// console.log('onclick pointsObj %o',cloudPointObj.points);
    if(globalMode!=MODE_EDIT_MARK){
		//选点模式
		// var intersects =cloudPointObj.checkItemClick(viewObj.scene,viewObj.camera,cloudPointObj.points);
		// if(intersects && intersects.length>0){
		// 	let pointArr=[];
		// 	let vec3 = intersects[0].point;
		// 	pointArr.push(vec3);
		// 	cloudPointObj.addMarkers(viewObj,pointArr,cloudPointObj.points.tfClient,'cyan',markerSize);

		// 	//进入方向选择
		// 	globalMode = MODE_DIRECT;
		// 	cloudPointObj.addAxe(viewObj,pointArr[0]);
		// }


		var targetVec3 =cloudPointObj.checkGroundClick(viewObj.scene,viewObj.camera,cloudPointObj.points);
		globalMode = MODE_DIRECT;
		cloudPointObj.addAxe(viewObj,targetVec3);
		cloudPointObj.addSingleMarker(viewObj,targetVec3);
		cloudPointObj.targetVec3 = targetVec3;
	}else{
		//已过时
		//编辑选点模式
		if(viewObj.groups[RECT_GROUP] ){
			
		}
		var intersects =cloudPointObj.checkRectSelect(viewObj.scene,viewObj.camera,viewObj.groups[RECT_GROUP].children);
		if(intersects.length>0){
			console.log(intersects[0].object.position);
			let position =intersects[0].object.position;
			//根据坐标删除框图和标记

			let rect = viewObj.getGroupElementByPosition(RECT_GROUP,position);
			viewObj.removeGroupElement(RECT_GROUP,rect);
			let marker = viewObj.getGroupElementByPosition(SELECT_OBJECTS_GROUP,position);
			viewObj.removeGroupElement(SELECT_OBJECTS_GROUP,marker);

			
		}
	}
	
	
	
	
}




// function addMarker(cloudObj,vector3){
// 	cloudObj.addMarker(vector3);
// }

